package org.csu.mypetstore.controller;
import net.sf.json.JSONObject;
import org.csu.mypetstore.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @GetMapping
    @ResponseBody
    public String get(String type, String id){
        if(!type.equals("id"))
        {
            JSONObject object = new JSONObject();
            object.put("status","OK");
            object.put("msg","大类列表获取成功!");
            object.put("data",categoryService.getCategoryList());
            return object.toString();
        }
        else
        {
            if(id.isEmpty())
            {
                JSONObject object = new JSONObject();
                object.put("status","OK");
                object.put("msg","大类信息获取失败!");
                return object.toString();
            }
            else
            {
                JSONObject object = new JSONObject();
                object.put("status","OK");
                object.put("msg","大类信息获取成功!");
                object.put("data",categoryService.getCategory(id));
                return object.toString();
            }
        }
    }
}
